package org.cloud.controller;


import org.cloud.entity.Dept;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer/dept")
public class ConsumerDeptController {
    // 定义请求地址
//    private static final String REST_URL_PREFIX = "http://localhost:8088";

    private static final String REST_URL_PREFIX = "http://spring-provider";  // 使用 Ribbon 服务名称访问
    @Resource // 注入 RestTemplate
    private RestTemplate restTemplate;

    @GetMapping("/discovery/{serviceId}")
    public Object discovery(@PathVariable("serviceId") String serviceId) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery/"+ serviceId , Object.class);
    }

    @RequestMapping("/list")
    public List<Dept> ListDept() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    @RequestMapping("/get/{id}")
    public Dept getDept(@PathVariable("id") Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/add")
    public boolean addDept(Dept dept){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/add",Boolean.class,dept);
    }


}
